﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using memo;
using memoVM;
using Microsoft.Win32;

namespace memoUI
{
    /// <summary>
    /// Interaction logic for KnowledgeTestEditorWindow.xaml
    /// </summary>
    public partial class KnowledgeTestEditorWindow : Window
    {
        private Test _Test;
        private bool isNew;
        private Binding _binding = new Binding();

        private ViewModel _ViewModel = ViewModel.Instance;

        /// <summary>
        /// Új Test létrehozásához
        /// </summary>
        public KnowledgeTestEditorWindow()
        {
            isNew = true;
            _Test = _ViewModel.GetNewDefaultTest();
            QuestionFileName = "DEFAULT.png";
            AnswerFileName = "DEFAULT.png";

            Init();
        }

        /// <summary>
        /// Már meglévő Test szerkesztéséhez 
        /// </summary>
        public KnowledgeTestEditorWindow(Test test)
        {
            isNew = false;
            _Test = test;

            if (_Test.QuestionType == Test.TestType.Image)
            {
                QuestionFileName = (string)_Test.Question;
            }
            else
            {
                QuestionFileName = "DEFAULT.png";
            }

            if (_Test.AnswerType == Test.TestType.Image)
            {
                AnswerFileName = (string)_Test.Answer;
            }
            else
            {
                AnswerFileName = "DEFAULT.png";
            }

            Init();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            _Test.QuestionType = (Test.TestType)QuestionTypeComboBox.SelectedItem;
            _Test.AnswerType = (Test.TestType)AnswerTypeComboBox.SelectedItem;
            
            if (_Test.QuestionType == Test.TestType.String)
            {
                _Test.Question = QuestionTextBox.Text;
            }
            else
            {
                _Test.Question = QuestionFileName;
            }

            if (_Test.AnswerType == Test.TestType.String)
            {
                _Test.Answer = AnswerTextBox.Text;
            }
            else
            {
                _Test.Answer = AnswerFileName;
            }


            if (isNew == true)
            {
                _ViewModel.SaveTest(_Test);
            }
            else
            {
                _ViewModel.EditTest(_Test);
            }

            this.Close();
        }

        private void Init()
        {
            InitializeComponent();

            //comboboxokhoz tartalom rendelése
            QuestionTypeComboBox.ItemsSource = Enum.GetValues(typeof(Test.TestType));
            AnswerTypeComboBox.ItemsSource = Enum.GetValues(typeof(Test.TestType));

            ShowQuestionControls();
            ShowAnswerControls();
        }

        /// <summary>
        /// A TestType-nak megfelelő controlok megjelenítése 
        /// </summary>
        private void ShowQuestionControls()
        {
            if (_Test.QuestionType == Test.TestType.String)
            {
                QuestionTypeComboBox.SelectedItem = Test.TestType.String;
                QuestionTextBox.Visibility = Visibility.Visible;
                QuestionImage.Visibility = Visibility.Hidden;

                QuestionTextBox.Text = (string)_Test.Question;
            }
            else
            {
                QuestionTypeComboBox.SelectedItem = Test.TestType.Image;
                QuestionTextBox.Visibility = Visibility.Hidden;
                QuestionImage.Visibility = Visibility.Visible;

                QuestionImage.Source = new BitmapImage(new Uri(@"Images/" + QuestionFileName, UriKind.RelativeOrAbsolute));
            }
        }

        /// <summary>
        /// A TestType-nak megfelelő controlok megjelenítése 
        /// </summary>
        private void ShowAnswerControls()
        {
            if (_Test.AnswerType == Test.TestType.String)
            {
                AnswerTypeComboBox.SelectedItem = Test.TestType.String;
                AnswerTextBox.Visibility = Visibility.Visible;
                AnswerImage.Visibility = Visibility.Hidden;

                AnswerTextBox.Text = (string)_Test.Answer;
            }
            else
            {
                AnswerTypeComboBox.SelectedItem = Test.TestType.Image;
                AnswerTextBox.Visibility = Visibility.Hidden;
                AnswerImage.Visibility = Visibility.Visible;

                AnswerImage.Source = new BitmapImage(new Uri(@"Images/" + AnswerFileName, UriKind.RelativeOrAbsolute));
            }
        }

        private void QuestionTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _Test.QuestionType = (Test.TestType)QuestionTypeComboBox.SelectedItem;
            ShowQuestionControls();
        }

        private void AnswerTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _Test.AnswerType = (Test.TestType)AnswerTypeComboBox.SelectedItem;
            ShowAnswerControls();
        }

        private void QuestionImage_MouseDown(object sender, MouseButtonEventArgs e)
        {
            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            // Set filter options and filter index.
            openFileDialog1.Filter = "Image files (*.jpg, *.jpeg, *.png) | *.jpg; *.jpeg; *.png";
            openFileDialog1.FilterIndex = 1;

            openFileDialog1.Multiselect = false;

            // Call the ShowDialog method to show the dialog box.
            bool? userClickedOK = openFileDialog1.ShowDialog();

            // Process input if the user clicked OK.
            if (userClickedOK == true)
            {
                QuestionFileName = openFileDialog1.SafeFileName;
                QuestionImage.Source = new BitmapImage(new Uri(@"Images/" + QuestionFileName, UriKind.RelativeOrAbsolute));
            }
        }

        private void AnswerImage_MouseDown(object sender, MouseButtonEventArgs e)
        {
            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            // Set filter options and filter index.
            openFileDialog1.Filter = "Image files (*.jpg, *.jpeg, *.png) | *.jpg; *.jpeg; *.png";
            openFileDialog1.FilterIndex = 1;

            openFileDialog1.Multiselect = false;

            // Call the ShowDialog method to show the dialog box.
            bool? userClickedOK = openFileDialog1.ShowDialog();

            // Process input if the user clicked OK.
            if (userClickedOK == true)
            {
                AnswerFileName = openFileDialog1.SafeFileName;
                AnswerImage.Source = new BitmapImage(new Uri(@"Images/" + AnswerFileName, UriKind.RelativeOrAbsolute));
            }
        }

        private string QuestionFileName;
        private string AnswerFileName;
    }
}
